<?php

namespace App\V3\V3_2_6\Models;

use App\Api\Models\SalesReturnPackageModel;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

/**
 * 退货
 * Class SalesReturnModel
 * @package App\Models
 */
class SalesReturnModel extends Model
{


    protected $table = 'sales_return';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储
    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
    /**
     * $dates 数组内的列转换为日期格式
     *
     * @var array
     */
    protected $dates = [
        'refund_date',
    ];

    public function returnMateriel()
    {
        return self::hasMany(SalesReturnGoodsDetailModel::class, 'return_id', 'id');
    }

    public function returnPackage()
    {
        return self::hasMany(SalesReturnPackageModel::class, 'return_id', 'id');
    }



    static public function getSalesIdInfo($userId, $data, $condition)
    {
        return self::select('id')
            ->from('sales_return as a')
            ->where($condition)
            ->where($userId)->get()->toArray();
    }
    //今日退货数量
    static public function getTodayReturnCount($condition, $startTime, $endTime)
    {
        return self::where($condition)
                ->where('delete_flag',0)
                ->whereBetween('created_at', [$startTime, $endTime])
                ->count();
    }
}
